package org.learn.file;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {
    public static void main(String[] args) {
        //完成拷贝
        //创建文件的输入流，读入到程序
        //创建文件的输出流，写入到指定的文件
        String srcFilePath = "d:\\图片\\dog.jpg";
        String destFilePath = "d:\\test\\dog.jpg";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;


        try {
            fileInputStream = new FileInputStream(srcFilePath);
            fileOutputStream = new FileOutputStream(destFilePath);
            //定义一个字节数组，提高读取速度
            byte[] buf = new byte[1024];
            int readLen = 0 ;
            while ((readLen = fileInputStream.read(buf)) != -1){
                //读取到后，就写入到文件 通过 fileOutputStream
                //即，是一边读一边写
                fileOutputStream.write(buf,0,readLen);


            }
            System.out.println("拷贝ok");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭输入流和输出流
                if(fileInputStream != null) {
                    fileInputStream.close();
                }
                if(fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
